package message

import (
	"context"
	"encoding/json"
	"wave-service/server/app/common/response"

	"wave-service/server/app/internal/svc"
	"wave-service/server/app/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MessageSetLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMessageSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MessageSetLogic {
	return &MessageSetLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MessageSetLogic) MessageSet(req *types.SetMsgReadRequest) (resp *types.SetMsgReadResponse, err error) {
	uid, _ := l.ctx.Value("uid").(json.Number).Int64()
	userContent, err := l.svcCtx.VisitorContactsModel.FindOneByUserId(l.ctx, uid)
	if err != nil {
		logx.Errorf("获得用户客户关系失败%v", err.Error())
		return nil, response.Error(500, "系统错误")
	}
	num, err := l.svcCtx.MessagesModel.UpdateMsgRead(userContent.ChannelId, userContent.VisitorId)
	//log.Println("设置消息为已读 err=", err)
	if err != nil {
		logx.Errorf("设置消息已读失败%v", err.Error())
		return nil, response.Error(500, "系统错误")
	}

	return &types.SetMsgReadResponse{Num: num}, nil
}
